package com.cindata

import grails.transaction.Transactional

@Transactional
class AccountFlowStageRoleService
{
    def initFlowStageRole(Account account, Workflow workflow)
    {
        def workflowStages = WorkflowStage.findAllByWorkflow(workflow)
        for (
            workflowStage in
                workflowStages)
        {
            workflowStage?.roles?.each {
                def accountFlowStageRole = new AccountFlowStageRole()
                accountFlowStageRole.role = it?.role
                accountFlowStageRole.teamRole = it?.teamRole
                accountFlowStageRole.flowStage = AccountFlowStage.findByAccountAndStage(account, workflowStage?.stage)
                if (accountFlowStageRole.validate())
                {
                    accountFlowStageRole.save flush: true
                }
                else
                {
                    println accountFlowStageRole.errors
                }
            }
        }
    }
}